package com.kexinxin.service.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import com.kexinxin.util.DBUtil;
import com.kexinxin.util.matching.EdgeNode;



public class MatchingDAOImpl {
	public static List<EdgeNode> getEdgeNodeList() {
		List<EdgeNode> edgeNOdeList = new LinkedList<EdgeNode>();
		String getEdgeNodeList = "SELECT * FROM BN";
		try {
			Connection connection = DBUtil.getConnection();
			PreparedStatement pStatement = connection.prepareStatement(getEdgeNodeList);
			ResultSet resultSet = pStatement.executeQuery();
			while (resultSet.next()) {
				EdgeNode edgeNode = new EdgeNode();
				edgeNode.u = resultSet.getInt(3);
				edgeNode.v = resultSet.getInt(4);
				edgeNode.startName = SearchDAOImpl.getSkillNameById(resultSet.getInt(3));
				edgeNode.endName = SearchDAOImpl.getSkillNameById(resultSet.getInt(4));
				edgeNode.weight = resultSet.getInt(5);
				edgeNOdeList.add(edgeNode);
			}
			DBUtil.closeResultSet(resultSet);
			DBUtil.closePreparedStatement(pStatement);
			DBUtil.closeConnection(connection);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return edgeNOdeList;
	}

	public static int getNodeNumber() {
		List<EdgeNode> edgeNodeList = getEdgeNodeList();
		Set<Integer> set = new HashSet<Integer>();
		for (EdgeNode edgeNode : edgeNodeList) {
			set.add(edgeNode.u);
			set.add(edgeNode.v);
		}
		return set.size();
	}

	public static int getNodeMaxNumber() {
		List<EdgeNode> edgeNodeList = getEdgeNodeList();
		int max = 0;
		for (EdgeNode edgeNode : edgeNodeList) {
			if (max < edgeNode.u) {
				max = edgeNode.u;
			}
			if (max < edgeNode.v) {
				max = edgeNode.v;
			}
		}
		return max;
	}
}
